<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

//引入cookie
use think\Cookie;

class Admin extends Backend {
    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->AdminModel = model( 'Admin' );
    }

    // 管理员列表

    public function index() {
        // 获取管理员用户的信息
        $adminList = $this->AdminModel->paginate( 10 );

        foreach ( $adminList as $item ) {
            if ( $item[ 'state' ] == 1 ) {
                $item[ 'statetxt' ] = '正常';
            } else if ( $item[ 'state' ] == 0 ) {
                $item[ 'statetxt' ] = '禁用';
            }
        }

        // 管理员的数量
        $adminCount = $this->AdminModel->count();

        // 分页
        $pagestr = $adminList->render();

        $this->assign( [
            'adminList' => $adminList,
            'adminCount' => $adminCount,
            'pagestr' => $pagestr
        ] );

        return $this->fetch();
    }

    // 改变管理员用户状态

    public function change() {
        $tips = [
            'result' => false,
            'msg' => '',
        ];

        // 获取管理员信息
        $adminId = $this->request->param( 'adminid' );

        // 判断管理员是否存在
        $adminInfo = $this->AdminModel->where( [ 'id' => $adminId ] )->find();
        if ( !$adminInfo ) {
            $tips[ 'msg' ] = '管理员不存在！';
            return $tips;
        }

        // 获取用户的状态
        $state = intval( $adminInfo[ 'state' ] );

        // 获取数据库所有的管理员信息
        $adminList = $this->AdminModel->select();

        $num = 0;

        foreach ( $adminList as $item ) {
            if ( $item[ 'state' ] == 1 ) {
                $num++;
            }
        }

        // 判断管理员是否被全部禁用
        if ( $num >= 2 || $state == 0 ) {

            // 修改状态
            if ( $state == 1 ) {
                $state = 0;
            } else if ( $state == 0 ) {
                $state = 1;
            }

            // 组装数据
            $data = [
                'id' => $adminId,
                'state' => $state,
            ];

            // 更改数据
            $result = $this->AdminModel->isUpdate()->save( $data );

            if ( $result === false ) {
                //修改失败
                $tips[ 'msg' ] = $this->AdminModel->getError();
                return $tips;
            } else {
                //修改成功
                $tips[ 'result' ] = true;
                $tips[ 'msg' ] = '修改成功';
                return $tips;
            }
        }
        $tips[ 'msg' ] = '修改失败，管理员不能少于一个！';

        return $tips;
    }

    // 修改管理员密码

    public function revise() {

        // 获取管理员信息
        $adminId = $this->request->param( 'adminid' );

        $this->assign( [
            'adminId'=>$adminId
        ] );

        if ( $this->request->isPost() ) {

            // 判断管理员是否存在
            $adminInfo = $this->AdminModel->where( [ 'id' => $adminId ] )->find();
            if ( !$adminInfo ) {
                $this->error( $this->AdminModel->getError() );
                exit;
            }

            // 获取新密码
            $newPwd = $this->request->param( 'pass' );

            // 密码加密
            $pwd = md5( $newPwd.$adminInfo[ 'salt' ] );

            // 组装数据
            $data = [
                'id' => $adminId,
                'password' => $pwd,
            ];

            // 更改数据
            $result = $this->AdminModel->isUpdate()->save( $data );

            if ( $result === false ) {
                //修改失败
                $this->error( $this->AdminModel->getError() );
                exit;
            } else {
                //修改成功
                $this->success( '密码修改成功！', url( 'admin/admin/index' ) );
                exit;
            }
        }
        return $this->fetch();
    }

    // 检测输入是否正确

    public function check() {
        $request = [
            'result' => false,
            'msg' => '',
        ];

        // 获取当前登录用户信息
        $adminId = $this->request->param( 'adminid' );

        // 获取用户其他信息
        $AdminInfo = $this->AdminModel->where( [ 'id' => $adminId ] )->find();

        if ( !$AdminInfo ) {
            $this->error( $this->AdminModel->getError() );
            exit;
        }

        if ( $this->request->isPost() ) {

            // 获取传来的的信息
            $params = $this->request->param();

            // 旧密码操作
            if ( !empty( $params[ 'oldpass' ] ) ) {
                //获取输入的密码
                $oldpass = $params[ 'oldpass' ];

                // 密码加密
                $oldpass = md5( $oldpass.$AdminInfo[ 'salt' ] );

                // 判断并操作
                if ( $oldpass == $AdminInfo[ 'password' ] ) {
                    $request[ 'result' ] = true;
                    $request[ 'msg' ] = '密码正确！继续操作！';
                    echo json_encode( $request, JSON_UNESCAPED_UNICODE );
                    exit;
                } else {
                    $request[ 'result' ] = false;
                    $request[ 'msg' ] = '密码错误！请重新输入！';
                    echo json_encode( $request, JSON_UNESCAPED_UNICODE );
                    exit;
                }
            }

            // 新密码检查
            if ( !empty( $params[ 'newpass' ] ) ) {
                //获取输入的密码
                $newpass = $params[ 'newpass' ];

                // 密码加密
                $newpass = md5( $newpass.$AdminInfo[ 'salt' ] );

                // 判断并操作
                if ( $newpass == $AdminInfo[ 'password' ] ) {
                    $request[ 'result' ] = false;
                    $request[ 'msg' ] = '密码重复！请重新输入！';
                    echo json_encode( $request, JSON_UNESCAPED_UNICODE );
                    exit;
                } else {
                    $request[ 'result' ] = true;
                    $request[ 'msg' ] = '密码不重复！继续操作！';
                    echo json_encode( $request, JSON_UNESCAPED_UNICODE );
                    exit;
                }
            }

        }

    }

    // 添加管理员

    public function add() {
        if ( $this->request->isPost() ) {

            $username = $this->request->param( 'username', '' );
            $password = $this->request->param( 'password', '' );
            $repassword = $this->request->param( 'repassword', '' );
            $nickname = $this->request->param( 'nickname', '' );

            // 判断密码和确认密码是否一致
            if ( $password !== $repassword ) {
                $this->error( '密码和确认密码不一致！' );
                exit;
            }

            // 生成密码盐
            $salt = randstr( 10 );

            // 密码加密
            $password = md5( $password.$salt );

            $data = [
                'username' => $username,
                'nickname' => $nickname,
                'password' => $password,
                'salt' => $salt,
            ];

            $res = $this->AdminModel->save( $data );

            if ( $res === false ) {
                $this->error( '添加管理员失败！' );
                exit;
            } else {
                $this->success( '添加管理员成功！', url( 'admin/admin/index' ) );
                exit;
            }
        }
        return $this->fetch();
    }

    // 检测输入是否正确

    public function usernamecheck() {
        $request = [
            'result' => false,
            'msg' => '',
        ];

        // 获取输入的分类名
        $name = $this->request->param( 'name' );

        // 获取全部分类信息
        $cateinList = $this->AdminModel->where( [ 'username' => $name ] )->find();

        if ( $cateinList ) {
            $request[ 'result' ] = false;
            $request[ 'msg' ] = '用户名已经存在，请重新输入！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        } else {
            $request[ 'result' ] = true;
            $request[ 'msg' ] = '用户名不存在，可以继续操作！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        }

    }

    // 检测输入是否正确

    public function nicknamecheck() {
        $request = [
            'result' => false,
            'msg' => '',
        ];

        // 获取输入的分类名
        $name = $this->request->param( 'name' );

        // 获取全部分类信息
        $cateinList = $this->AdminModel->where( [ 'nickname' => $name ] )->find();

        if ( $cateinList ) {
            $request[ 'result' ] = false;
            $request[ 'msg' ] = '用户名已经存在，请重新输入！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        } else {
            $request[ 'result' ] = true;
            $request[ 'msg' ] = '用户名不存在，可以继续操作！';
            echo json_encode( $request, JSON_UNESCAPED_UNICODE );
            exit;
        }

    }

    //

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 真删除
            $result = $this->AdminModel->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ConfigModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除管理员成功';
            }

            return json( $success );
        }
        return '456';
    }

}
?>